package sync.model.auto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.beanutils.BeanUtils;

/**
 * 自动获取变动数据到缓存库中
 * 
 * @author lili
 * 
 */
public class AutoDbToCache implements Runnable {
	@SuppressWarnings("unchecked")
	@Override
	public void run() {
		try {
			System.out.println("缓存者...");
			List<LocalEntity> dbList = new ArrayList<LocalEntity>();
			for (Map.Entry<String, LocalEntity> entry : DataBase.localDB
					.entrySet()) {
				if (entry.getValue().getStatus().equals("1")) {
					LocalEntity clone = (LocalEntity) BeanUtils.cloneBean(entry
							.getValue());
					dbList.add(clone);
					entry.getValue().setStatus("-1");// 改变实体状态为等待上传中
				}
			}
			// 根据修改日期排序
			Collections.sort(dbList, new BeanComparator("modifyTime"));
			for (int i = 0, j = 0; i < dbList.size(); i++) {// 每次最多取3个修改
				LocalEntity entity = dbList.get(i);
				DataBase.cache.put(entity);
				System.out.println("◇◇◇缓存修改项，编号：" + entity.getId() + "，修改时间："
						+ entity.getModifyTime().getTime() + "...");
				if (j++ == 2) {
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
